package com.hopedove.workshop.ui.shiftchange

import android.app.DatePickerDialog
import android.os.Bundle
import android.widget.CheckBox
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.base.BaseActivity
import com.hopedove.data.Datetime
import com.hopedove.viewmodel.CommonEvent
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ObservableShiftQueryItem
import com.hopedove.workshop.databinding.WorkshopShiftChangeQueryBinding
import com.hopedove.workshop.ui.adapter.ShiftChangeQueryAdapter
import com.hopedove.workshop.viewmodel.shiftchange.ShiftChangeQueryViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

class ShiftChangeQueryActivity : BaseActivity() {

    lateinit var binding: WorkshopShiftChangeQueryBinding
    private val viewModel: ShiftChangeQueryViewModel by viewModel()
    private val mAdapter = ShiftChangeQueryAdapter()

    private var datePickerDialog: DatePickerDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.workshop_shift_change_query)
        binding.lifecycleOwner = this
        binding.vm = viewModel

        initRecyclerView()
        observeToastText(viewModel.toastText)
        observeNavigation(viewModel.navigation)
        observeStartPickDate(viewModel.pickStartDate)
        observeEndPickDate(viewModel.pickEndDate)
        observeQueryItems(viewModel.queryItems)
        observeCommonEvent(viewModel.commonEvent)
        viewModel.load()
    }


    private fun initRecyclerView() {
        with(binding.rlList) {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }

        binding.listHeader.tvCheck.setOnClickListener {
            val check = (it as CheckBox).isChecked
            mAdapter.setAllCheckStatus(check)
        }

        mAdapter.allCheckedListener = {
            binding.listHeader.tvCheck.isChecked = it
        }

    }

    private fun observeCommonEvent(liveData: LiveData<CommonEvent<*>>) {
        liveData.observe(this) { event ->
            when (event.name) {
                "set_all_check_status" -> {
                    val status = event.data as Boolean
                    binding.listHeader.tvCheck.isChecked = status
                    mAdapter.setAllCheckStatus(status)
                }
            }
        }
    }

    private fun observeQueryItems(liveData: LiveData<List<ObservableShiftQueryItem>>) {
        liveData.observe(this) { data ->
            mAdapter.setData(data)
        }
    }

    private fun observeStartPickDate(event: LiveData<Datetime>) {
        event.observe(this, { date ->
            datePickerDialog?.dismiss()
            datePickerDialog = DatePickerDialog(
                this,
                datePickerListener,
                date.year(),
                date.month() - 1,
                date.dayOfMonth()
            )
            datePickerDialog?.updateDate(date.year(), date.month() - 1, date.dayOfMonth())
            datePickerDialog?.show()
        })
    }

    private fun observeEndPickDate(event: LiveData<Datetime>) {
        event.observe(this) { date ->
            datePickerDialog?.dismiss()
            datePickerDialog = DatePickerDialog(
                this,
                endDatePickListener,
                date.year(),
                date.month() - 1,
                date.dayOfMonth()
            )
            datePickerDialog?.updateDate(date.year(), date.month() - 1, date.dayOfMonth())
            datePickerDialog?.show()
        }
    }

    private val datePickerListener =
        DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            viewModel.updateStartDate(Datetime(year, month + 1, dayOfMonth))
        }

    private val endDatePickListener =
        DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            viewModel.updateEndDate(Datetime(year, month + 1, dayOfMonth))
        }

}